package gui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Main {
	protected static int delay = 60;
	private static Timer timer;
	// Panel/frame with statistic

	public final static int WIDTH_OF_FIELD = 500;
	public final static int HEIGHT_OF_FIELD = 350;

	private static void start() {
		timer.start();
	}

	private static void stop() {
		timer.stop();
	}

	public static Timer getTimer() {
		return timer;
	}

	public static void setTimer(Timer timer) {
		Main.timer = timer;
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				new ListOfPerson();

				// Default
				JFrame frame = new JFrame("Simulation Demo");
				Container pane = frame.getContentPane();
				GridBagLayout gbl = new GridBagLayout();
				frame.setLayout(gbl);

				// Main field
				DrawPerson p = new DrawPerson();
				p.setPreferredSize(new Dimension(WIDTH_OF_FIELD, HEIGHT_OF_FIELD));
				timer = new Timer(delay, p);

				pane.add(p, addFieldGbc());

				GridBagConstraints settings = addSettingsGbc();
				// Sliders
				Sliders s = addSlidersWithLabels(p, pane, settings);
				// Program Discription
				addDiscription(settings, pane);

				// Add button

				addStartStopButton(s.getNumbOfPeoples(), s.getNumbOfZombies(), settings, pane);

				// Default settings
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//				frame.setSize(1400, 750);
				frame.setSize(950, 600);
				frame.setResizable(false);
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);

			}

		});
	}

	private static void addDiscription(GridBagConstraints settings, Container pane) {
		settings.anchor = GridBagConstraints.CENTER;
		settings.insets = new Insets(100, 0, 0, 10);
		JLabel t3 = new JLabel("This is a simple simulation");
		pane.add(t3, settings);
		settings.insets = new Insets(10, 0, 0, 10);
		JLabel t4 = new JLabel("<html> of the real world. <font color='red'><b>Red </b></font>circles</html>");
		pane.add(t4, settings);
		JLabel t5 = new JLabel("<html> are people.<font color='black'><b> Black</b></font> are zombies.</html>");
		settings.anchor = GridBagConstraints.CENTER;
		JLabel t6 = new JLabel("First set the number of units!");
		pane.add(t5, settings);
		pane.add(t6, settings);

	}

	private static void addStartStopButton(JSlider numbOfPeoples, JSlider numbOfZombies, GridBagConstraints settings,
			Container pane) {
		JButton button = new JButton("Start");
		button.addActionListener(new ActionListener() {
			private boolean moving = false;

			@Override
			public void actionPerformed(ActionEvent e) {
				if (moving) {
					moving = false;
					stop();
					button.setText("Start");
				} else {
					if (numbOfPeoples.getValue() != 0 || numbOfZombies.getValue() != 0) {
						moving = true;
						start();
						button.setText("Stop");
					} else {

						JOptionPane.showMessageDialog(new JFrame(), "Please set the number of units!");

					}

				}
			}
		});
		settings.anchor = GridBagConstraints.CENTER;
		pane.add(button, settings);
	}

	private static Sliders addSlidersWithLabels(DrawPerson p, Container pane, GridBagConstraints settings) {
		// People Slider
		Sliders s = new Sliders(p);

		JLabel t1 = new JLabel("Set the number of Peoples: ");
		pane.add(t1, settings);

		JSlider numbOfPeoples = s.getNumbOfPeoples();

		pane.add(numbOfPeoples, settings);

		// Zombie Slider

		JLabel t2 = new JLabel("Set the number of Zombies: ");
		pane.add(t2, settings);

		JSlider numbOfZombies = s.getNumbOfZombies();

		pane.add(numbOfZombies, settings);

		// Speed Slider

		JLabel t7 = new JLabel("Set Speed: ");
		pane.add(t7, settings);

		JSlider speed = s.getSpeed();

		pane.add(speed, settings);
		return s;

	}

	private static GridBagConstraints addFieldGbc() {
		GridBagConstraints fieldGbc = HelpGBC.makegbc(GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE,
				GridBagConstraints.REMAINDER, GridBagConstraints.REMAINDER);
		fieldGbc.fill = GridBagConstraints.BOTH;
		fieldGbc.anchor = GridBagConstraints.CENTER;
		fieldGbc.insets = new Insets(0, 0, 0, 10);
		return fieldGbc;

	}

	private static GridBagConstraints addSettingsGbc() {
		GridBagConstraints settings = HelpGBC.makegbc(GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE,
				GridBagConstraints.REMAINDER, 1);
		settings.anchor = GridBagConstraints.NORTH;
		settings.insets = new Insets(5, 0, 5, 0);
		return settings;
	}

}
